<?php

namespace app\index\controller;

use app\common\IndexController;
use app\index\model\User as UserModel;
use app\index\model\Article;
use app\index\model\Like;

class Personal extends IndexController
{
    //编辑用户信息
    public function edit()
    {
        $data = $this->request->param();
        $id = $this->userid;
        $user = new UserModel();
        $res = $user->where('id', $id)->save($data);
        if ($res) {
            return success('修改成功');
        }
        return error('修改失败');
    }

    //获取用自己发布的文章
    public function getMyArticle()
    {
        $user_id = $this->userid;
        $limit = $this->request->param('limit', 10);
        $page = $this->request->param('page', 1);
        $data = Article::with(['user'])
            ->where('user_id', $user_id)
            ->order('create_time', 'desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($data) {
            return success('获取成功', $data);
        } else {
            return error('获取失败');
        }
    }

    //获取我点赞的文章
    public function getMyLike()
    {
        $user_id = $this->userid;
        $limit = $this->request->param('limit', 10);
        $page = $this->request->param('page', 1);

        $likeQuery = Like::where('user_id', $user_id);
        $article_ids = $likeQuery->column('article_id');
        if (!empty($article_ids)) {
            $data = Article::with(['user'])
                ->where('article_id', 'in', $article_ids)
                ->order('create_time', 'desc')
                ->paginate([
                    'list_rows' => $limit,
                    'page' => $page
                ]);
            return success('获取成功', $data);
        } else {
            return success('暂无点赞文章', []);
        }
    }
}
